package com.anne.fish.service.img;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;

public class ImageIOCompress implements ImageCompress {
	public BufferedImage readImage(String source) throws IOException {
		return ImageIO.read(new File(source));
	}

	public void compress(BufferedImage image, String target) throws IOException {
		File targetFile = new File(target);
		ImageWriter writer = null;
		ImageOutputStream outputStream = null;
		try {
			ImageTypeSpecifier type = ImageTypeSpecifier
					.createFromRenderedImage(image);
			Iterator<ImageWriter> iterator = ImageIO.getImageWriters(type,
					"JPEG");
			if (!iterator.hasNext()) {
				return;
			}
			writer = iterator.next();
			IIOImage iioImage = new IIOImage(image, null, null);
			ImageWriteParam param = writer.getDefaultWriteParam();
			param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
			param.setCompressionQuality(0.5f);
			outputStream = ImageIO.createImageOutputStream(targetFile);
			writer.setOutput(outputStream);
			writer.write(null, iioImage, param);
		} finally {
			if (outputStream != null) {
				outputStream.close();
			}
			if (writer != null) {
				writer.abort();
			}
		}
	}

	/**
	 * 
	 * @param file
	 * @param target
	 * @param width
	 * @param height
	 * @return
	 * @throws IOException
	 */
	public File compress(File file, String target, int width, int height,
			String imageType) throws Exception {
		File tf = new File(target);
		try {

			Image image = ImageIO.read(file);
			BufferedImage tag = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			tag.getGraphics().drawImage(image, 0, 0, width, width, null); // 绘制缩小后的图
			Iterator<ImageWriter> it = ImageIO
					.getImageWritersBySuffix(imageType);
			ImageWriter iw = (ImageWriter) it.next();
			FileImageOutputStream fileImageOutputStream = new FileImageOutputStream(
					tf);

			ImageWriteParam iwp = iw.getDefaultWriteParam();
			iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
			iwp.setCompressionQuality(1);
			iw.setOutput(fileImageOutputStream);
			iw.write(null, new IIOImage(tag, null, null), iwp);
			iw.dispose();
			fileImageOutputStream.flush();
			fileImageOutputStream.close();

		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {

		}
		return tf;
	}

	public byte[] compress(byte[] data,int width, int height,String imageType) throws Exception {
		float quality = 1f;
		
		
		ByteArrayInputStream is = new ByteArrayInputStream(data);

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {

			Image image = ImageIO.read(is);
			BufferedImage tag = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			tag.getGraphics().drawImage(image, 0, 0, width, width, null); 
			 
			ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix(imageType).next();
			ImageWriteParam iwp = iw.getDefaultWriteParam();
			iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
			iwp.setCompressionQuality(quality);
			iw.setOutput(ImageIO.createImageOutputStream(out));
			iw.write(null, new IIOImage(tag, null, null), iwp);

			out.flush();
			out.close();
			is.close();
			data = out.toByteArray();

		}  catch (IOException ex) {
			ex.printStackTrace();
		} finally {

		}
		return data;
	}

	public static void main(String[] args) throws Exception {
		ImageIOCompress compress = new ImageIOCompress();
		// compress.compress(compress.readImage("d:/upload/DSC01176.JPG"),
		// "d:/aa.jpg");
		File tf = compress.compress(new File("d:/upload/DSC01176.JPG"),
				"d:/aa.jpg", 360, 360, "");
		System.out.print(tf);
		tf.delete();
	}

}
